package com.eben.model;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

/**
 * This class represent how much "life" something have.
 * The Life is represented as a value between 0 and 100.
 * 
 * @author Jacob
 *
 */
public class Life {
	private int max = 100; // make it easier later if we need to change the range that a life should represent.
	private int life;
	private boolean isInvincible;
	private Position displayDimensions;
	RectF outer, inner;
	private int red;
	private int green;
	
	Paint innerPaint = new Paint();
	Paint outerPaint = new Paint();
	
	/**
	 * Give you the maximun allowed life.
	 */
	public Life(){
		this.life = max;
		this.isInvincible = false;
	}
	
	/**
	 * Constructor that takes a position and sets it as the displayDimension in use.
	 * @param position The dimensions of the display.
	 */
	public Life(Position position) {
		this.life = max;
		displayDimensions = position;
		outer = new RectF((float) ((displayDimensions.getX()/2)-(max/2)),20,(float) ((displayDimensions.getX()/2)-(max/2))+101,30);
		inner = new RectF((float) ((displayDimensions.getX()/2)-(max/2))+1,20+1,(float) ((displayDimensions.getX()/2)-(max/2))+getLife(),30-1);
	}
	
	/**
	 * Set how much life you want from the start.
	 * 
	 * @param life is a integer between 0 and 100. 0 and 100 is allowed values
	 */
	public Life(int life){
		this.isInvincible = false;
		if(life >= 0 && life <= max){
			this.life = life;
		}else{
			life = max;
		}	
	}
	
	/**
	 * Add life.
	 * 
	 * @param add - how much you want to add. Need to be greater than 0 
	 */
	public void addLife(int add){
		if(add > 0){
			this.life += add;
			if(this.life > max){
				this.life = max;
			}
		}
	}
	/**
	 * Remove Life
	 * 
	 * @param sub - how much you want to remove. Need to be greater than 0.
	 */
	public void subtractLife(int sub){
		if(isInvincible() == false)
			if(sub > 0){
				this.life -= sub;
				if(this.life < 0){
					this.life = 0;
				}
			}
	}
	
	/**
	 * Tell you if your object has run out of life.
	 * 
	 * @return False if life greater than 0. 
	 * else true.
	 */
	public boolean isDead(){
		if(this.life > 0){
			return false;
		}
		return true;
	}
	
	/**
	 * Set the players invincibilty
	 * 
	 * @param isInvincible boolean if invincibilty should be true or false
	 */
	public void setInvincible(boolean isInvincible){
		this.isInvincible = isInvincible;
	}
	
	/**
	 * Tells you if the player is invincible or not
	 * 
	 * @return Returns a boolean value depending if the player is invincible or not
	 */
	public boolean isInvincible(){
		return isInvincible;
	}
	
	/**
	 * Tells you how much life the player has left
	 * 
	 * @return The life as an int
	 */
	public int getLife() {
		return life;
	}
	
	/**
	 * Draws a bar which show the life of the player. The color of the life bar
	 * gradually goes from green to red depending on how much life the player have left
	 * 
	 * @param canvas canvas which to draw upon
	 */
	public void draw(Canvas canvas) {
		inner.set((float) ((displayDimensions.getX()/2)-(max/2))+1,20+1,(float) ((displayDimensions.getX()/2)-(max/2))+life,30-1);
		
		
		if(life >= 50){
			green = 240;						//When life is above 50 green is always 240 (visually nicer than 255 I think)
			red   = (int) ((-4.8*life)+480);	//When life is above 50 red starts at 0 (green), and grows to 240 the closer to 50 life gets (yellow)
		}else if(life < 50){
			red   = 240;						//The same as before, red just takes over
			green = (int) (4.8*life);			//The green color decreases until 0 (red)
		}
		
		innerPaint.setColor(Color.rgb(red, green, 0)); //the color blue is never used (green to red)
		outerPaint.setColor(Color.WHITE);
		
		canvas.drawRect(outer, outerPaint);
		canvas.drawRect(inner, innerPaint);
	}
	
	
	/**
	 * Draws a bar which show the time left on the invincibility
	 * 
	 * @param canvas canvas which to draw upon
	 * @param time the time left on the invincibility
	 */
	public void drawInvincibleTime(Canvas canvas, float time){
		inner.set((float) ((displayDimensions.getX()/2)-(max/2))+1,20+1,
				(float)(((displayDimensions.getX()/2)-(max/2))+(float)(100-time*100/InvinciblePoint.getActiveTime())),30-1);
		innerPaint.setColor(Color.BLUE);
		outerPaint.setColor(Color.WHITE);
		
		canvas.drawRect(outer, outerPaint);
		canvas.drawRect(inner, innerPaint);
	}
	
}
